﻿using AutoMapper;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using Tools.Core.Dtos;
using Tools.Core.Entities;
using Tools.Services.Interfaces;

namespace Tools.Modules.Dialogs.ViewModels
{
    public class MailSenderViewModel : BindableBase,IDialogAware
    {
        private readonly IMailSenderService _mss;
        private readonly IMapper _mapper;
        private MailSenderDto _sender;
        public MailSenderDto Sender
        {
            get => _sender;
            set=>SetProperty(ref _sender, value);
        }
        public DelegateCommand SubmitSenderCommand { get; private set; }
        public MailSenderViewModel(IMailSenderService mss,IMapper mapper)
        {
            _mss = mss;
            _mapper = mapper;
            SubmitSenderCommand = new DelegateCommand(() =>
            {
                var s = _mapper.Map<MailSender>(Sender);
                _mss.UpdateSenderAsync(s);
            });
        }

        public string Title => "邮件发送人";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            RequestClose?.Invoke(null);
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            var sender = parameters.GetValue<MailSenderDto>("sender");
            if (sender != null)
            {
                Sender = sender;
            }
        }
    }
}
